﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
namespace CommonLib
{
	public static class EditorUtil 
	{

		static public string GetSelectFolder()
		{
			string path = "Assets";
			UnityEngine.Object[] assets = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets);
			for (int index = 0; index < assets.Length; index++)
			{
				string assetPath = AssetDatabase.GetAssetPath(assets[index]);
				if (!string.IsNullOrEmpty(assetPath) && File.Exists(assetPath))
				{
					//所选的是文件
					path = Path.GetDirectoryName(assetPath);
					break;
				}
				else if(!string.IsNullOrEmpty(assetPath) && Directory.Exists(assetPath))
				{
					//所选的是文件夹
					path = assetPath;
					break;
				}
			}
			return path;
		}

		static public T GetSelectT<T>() where T : UnityEngine.Object
		{
			//T ret = default;
			if(TryGetSelectT(out T ret))
				return ret;
			else
				return default;
		}

		static public bool TryGetSelectT<T>(out T ret) where T : UnityEngine.Object
		{
			ret = default;
			UnityEngine.Object asset = Selection.activeObject;
			string assetPath = AssetDatabase.GetAssetPath(asset);
			if (!string.IsNullOrEmpty(assetPath) && File.Exists(assetPath))
			{
				ret = asset as T;
				return true;
			}
			else
				return false;
		}


		static public void GetAllAssets<T>(string path, string filter, ref List<T> list) where T : UnityEngine.Object
		{
			//获取所有符合名称要求的资源Guid
			string[] assets = AssetDatabase.FindAssets(filter, new string[] { path, });
			foreach (var item in assets)
			{
				//检查类型是否符合要求
				T getItem = AssetDatabase.LoadAssetAtPath<T>(AssetDatabase.GUIDToAssetPath(item));
				if (getItem != null)
				{
					list.Add(getItem);
				}
			}
		}
	}
}